$NG.AllReady(function (page, { useAction, useValuesChange, useUpdateRow, useDataIndexChange, useUpdateRows, useClick }) {
    var mstform = $NG.getCmpApi("p_form_detailed_design_chg"); //获取表头数据
    var dgrid = $NG.getCmpApi("p_form_detailed_design_dchg"); //获取表体数据
    var toolbar_dgrid = $NG.getCmpApi("toolbar_p_form_detailed_design_dchg")
    ////////////////////////////////////////////////////////////--toolbar--////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////--toolbar--////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////--toolbar--////////////////////////////////////////////////////////////

    const toolbars = [
        // { toolbar: $NG.getCmpApi("toolbar"), items: ["copy", "attachment", "check", "history", "applycheck"] },
        // { toolbar: $NG.getCmpApi("toolbar_top"), items: ["imp", "imp_push", "check", "history", "applycheck"] },
        { toolbar: $NG.getCmpApi("toolbar_p_form_detailed_design_dchg"), items: ["attachment", "imp"] },
    ];

    toolbars.forEach(({ toolbar, items }) => {
        if (toolbar) {
            items
                .map((item) => toolbar.getItem(item)) // 提前获取所有按钮
                .filter(Boolean) // 过滤掉不存在的按钮
                .forEach((toolbarItem) => toolbarItem.setHidden()); // 隐藏按钮
        }
    });


    toolbar_dgrid.insert({
        id: "addeq",
        text: "引用设备清单",
        iconCls: "icon-New",
    });

    ////////////////////////////////////////////////////////////--mstform--////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////--mstform--////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////--mstform--////////////////////////////////////////////////////////////
    if (page.oType == "add" || page.oType == "edit" || page.oType == "copy") {
        filterCntMain();
        if (mstform.getItem("phid_pc").getValue()) {
            getProject();
            getCntCount("p_form_detailed_design_chg", "u_cnt", () => {
                getCntInfo();
                getCntMInfoByPhid()
                getShsjHeadInfo()
            });
            getEqInfoByPhid_pc()
        }
    }
    useClick(() => {
        addEqClick();
    }, "addeq")
    //表头字段变化逻辑BEGIN
    {
        useValuesChange(({ args }) => {
            cleanCntInfo();
            filterCntMain();
            getProject();
            getCntCount("p_form_detailed_design_chg", "u_cnt", () => {
                getCntInfo();
                getCntMInfoByPhid()
            });
            shqdFilter()
            getEqInfoByPhid_pc()
            getShsjHeadInfo()
        }, "p_form_detailed_design_chg.phid_pc");
        useValuesChange(({ args }) => {
            getCntInfo();
        }, "p_form_detailed_design_chg.u_cnt");
        //监听深化设计
        useValuesChange(({ args }) => {
            getShsjInfoDetail()
            getShsjHeadInfo()
        }, "p_form_detailed_design_chg.u_shsjqd")
        //监听深化设计类型
        useValuesChange(({ args }) => {
            fieldSetHidden();
        }, "p_form_detailed_design_chg.u_detailed_type");
        // 监听是否影响工期
        //   useValuesChange(({ args }) => {
        //     console.log(`工期监听`, args);
        //     const _value = mstform.getItem("u_is_impact_schedule").getValue();
        //     console.log(`是否影响工期`, _value);
        //     let lock = false;
        //     if (Number(_value) == 1) {
        //       lock = true;
        //     } else {
        //       lock = false;
        //     }
        //     $NG.updateUI(function (updater, state) {
        //       updater.fieldSetForm.p_form_detailed_design_chg.u_is_adjust_schedule.setProps({
        //         required: lock, //required是否必输
        //       });
        //     });
        //   }, "p_form_detailed_design_chg.u_is_impact_schedule");
    } //表头字段变化逻辑END

    useUpdateRows(async ({ args }) => {
        let pro = []
        args[0].forEach(item => {

            pro.push(getEqDInfoByphid(item))
        })
        const res = await Promise.all(pro)
        caculatedgrid()
    }, "p_form_detailed_design_dchg")

    useUpdateRow(({ args }) => {
        checkData(args[0])
    }, "p_form_detailed_design_dchg")


    ////////////////////////////////////////////////////////////--mstform_function--////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////--mstform_function--////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////--mstform_function--////////////////////////////////////////////////////////////

    function checkData(record) {
        // 如果没有找到符合条件的工作单元，再进行清单数量的校验
        record.u_detailed_qty = record._init_total_qty - record.u_detailed_qty_ori + record.u_detailed_qty_this
        if (Number(record.u_detailed_qty_this) + Number(record.u_detailed_qty) - Number(record.u_detailed_qty) > record.u_work_unit_qty) {
            $NG.alert(`设备清单 ${record.u_equipment_list_no} 的深化数量 ${Number(record.u_detailed_qty_this) + Number(record.u_detailed_qty) - Number(record.u_detailed_qty)} 超出工作单元数量 ${record.u_work_unit_qty}`);
            record.u_detailed_qty_this = null;
            record.u_pur_cost_detailed_this = null;
            record.u_sub_cost_detailed_this = null;
            record.u_self_cost_detailed_this = null;
            return; // 终止后续计算，避免不必要的操作
        }
    }

    function getCntMInfoByPhid(params) {
        $NG.execServer("getCntMInfoByPhid", { phid: mstform.getItem("u_cnt").getValue() }, function (res) {
            const datas = JSON.parse(res.data)[0].extendObjects;
            $NG.updateState((updater) => {
                updater.data.p_form_detailed_design_chg.setProps({
                    u_cnt_no: datas.pcm_no,
                });
            });
        });
    }
    ////////////////////////////////////////////////////////////--dgrid--////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////--dgrid--////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////--dgrid--////////////////////////////////////////////////////////////
    if (dgrid) {
    }
    {
        //表体订阅字段更新逻辑BEGIN
        // 统一的更新逻辑函数
        const updateCosts = (args) => {
            console.log("updateCosts begin");
            args[0].u_sub_detailed_cost_this = (args[0].u_detailed_qty_this || 0) * (args[0].u_sub_prc || 0);
            args[0].u_sub_detailed_cost = (args[0].u_detailed_qty || 0) * (args[0].u_sub_prc || 0);

            args[0].u_pur_cost_detailed_this = (args[0].u_detailed_qty_this || 0) * (args[0].u_pur_prc || 0);
            args[0].u_pur_cost_detailed = (args[0].u_detailed_qty || 0) * (args[0].u_pur_prc || 0);

            args[0].u_self_cost_detailed_this = (args[0].u_detailed_qty_this || 0) * (args[0].u_self_prc || 0);
            args[0].u_self_cost_detailed = (args[0].u_detailed_qty || 0) * (args[0].u_self_prc || 0);
        };

        // 用于绑定触发字段和更新的通用函数
        const createChangeHandler = (fields) => {
            fields.forEach((field) => {
                useDataIndexChange(({ args, instance }) => {
                    updateCosts(args);
                    instance.updateRow(args[0]);
                    caculatedgrid();
                }, field);
            });
        };
        // 绑定字段变化以触发更新
        createChangeHandler(["p_form_detailed_design_dchg.u_detailed_qty_this", "p_form_detailed_design_dchg.u_detailed_qty"]);

        useDataIndexChange(({ args, instance }) => { }, "u_detailed_qty");
        useDataIndexChange(({ args, instance }) => {
            // caculatedgrid();
        }, "u_detailed_qty_this");
    } //表体订阅字段更新逻辑END

    // 方法定义
    ////////////////////////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////////////////////////

    function addEqClick() {
        if (!mstform.getItem("phid_pc").getValue()) {
            $NG.alert("请先选择项目");
            return;
        }

        let sqlFilter = `m.phid_pc = ${mstform.getValues().phid_pc}  and gzdy.u_point_qty>=0 and (shsj.u_detailed_qty_this is null or shsj.u_detailed_qty_this<gzdy.u_point_qty) `;

        $NG.external
            .openNGHelp({
                type: "NGMultipleHelp",
                helpId: "sbqd",
                valueField: "phid",
                labelField: "d.u_equipment_list_name",
                clientSqlFilter: sqlFilter,
            })
            .then((data) => {
                if (Array.isArray(data) && data.length > 0) {
                    console.log(data);
                    var gridArr = [];
                    var gridCount = data.length;
                    console.log(gridCount);
                    for (var i = 0; i < gridCount; i++) {
                        var gridObj = {};
                        gridObj.u_equipment_item = data[i].origin.phid;
                        gridObj.u_equipment_item_EXName = data[i].origin.u_equipment_list_no || "";
                        gridObj.u_work_unit_qty = data[i].origin.u_point_qty || 0;
                        gridObj.u_detailed_qty = (data[i].origin.u_detailed_qty_this || 0);
                        gridObj.u_u_total_shsj = data[i].origin.u_detailed_qty_this || 0;
                        gridObj.u_detailed_qty_this = (data[i].origin.u_point_qty || 0) - (data[i].origin.u_detailed_qty_this || 0);
                        gridArr.push(gridObj);
                    }
                    console.log(gridArr);
                    dgrid.addRows(gridArr);
                    dgrid.refreshView();
                }
            });
    }


    //根据项目筛选合同
    function filterCntMain() {
        console.log(`项目id`, mstform.getItem("phid_pc").getValue());
        $NG.updateUI((updater, state) => {
            console.log("过滤参数检查", updater);
            updater.fieldSetForm.p_form_detailed_design_chg.u_cnt.setProps({
                onBeforeOpen: async () => {
                    var phid_pc = mstform.getItem("phid_pc").getValue();
                    if (!phid_pc) {
                        $NG.alert("请先选择项目");
                        return false;
                    } else {
                        return true;
                    }
                },
                ...(!!mstform.getItem("phid_pc").getValue() && {
                    clientSqlFilter: {
                        bill_type: 4, //合同类型
                        phid_pc: mstform.getItem("phid_pc").getValue(),
                    },
                }),
            });
        });
    }
    // 获取项目信息
    function getProject() {
        $NG.execServer("getPtInfoByPhid", { phid: mstform.getItem("phid_pc").getValue() }, function (res) {
            const datas = JSON.parse(res.data)[0].extendObjects;
            console.log(`项目信息获取`, datas);
            $NG.updateState((updater) => {
                updater.data.p_form_detailed_design_chg.setProps({
                    u_equipment_list_total_cost: datas.u_total_cost_amt,
                    u_equipment_list_sub_cost: datas.u_sub_cost_amt,
                    u_equipment_list_pur_cost: datas.u_pur_cost_amt,
                    u_equipment_list_self_cost: datas.u_self_cost,
                    u_equipment_list_impl_amt: datas.u_implement_amt,
                    u_equipment_list_svc_amt: datas.u_svc_cost_amt,
                    u_pc_no: datas.pc_no,
                    u_sqgw: {
                        //售前顾问
                        value: datas.user_sqgw_phid,
                        label: datas.user_sqgw_name,
                    },
                    u_project_manager: {
                        //项目经理
                        value: datas.project_manager_phid,
                        label: datas.project_manager_name,
                    },
                    u_customer: {
                        //客户经理
                        value: datas.project_manager_phid,
                        label: datas.project_manager_name,
                    },
                });
            });
        });
    }
    //合同信息获取
    function getCntInfo() {
        $NG.execServer("getCntMInfoByPhid", { phid: mstform.getItem("u_cnt").getValue() }, function (res) {
            const datas = JSON.parse(res.data)[0].extendObjects;
            console.log(`合同信息获取`, datas);
            $NG.updateState((updater) => {
                updater.data.p_form_detailed_design_chg.setProps({
                    u_cnt_no: datas.pcm_no, //合同编码
                    u_cnt_revenue: datas.cnt_sum_fc, //合同收入
                    u_customer: {
                        // 客户名称
                        value: datas.customer_id,
                        label: datas.customer_name,
                    },
                });
            });
        });
    }
    //合同信息清空
    function cleanCntInfo(params) {
        $NG.updateState((updater) => {
            updater.data.p_form_detailed_design_chg.setProps({
                u_cnt_no: "", //合同编码
                u_cnt: "", //合同选择
                u_cnt_revenue: "", //合同收入
                u_customer: {
                    // 客户名称
                    value: "",
                    label: "",
                },
            });
        });
    }

    //深化清单过滤
    shqdFilter()
    function shqdFilter() {
        $NG.updateUI((updater) => {
            updater.fieldSetForm.p_form_detailed_design_chg.u_shsjqd.setProps({
                onBeforeOpen: async () => {
                    var phid_pc = mstform.getItem("phid_pc").getValue();
                    if (!phid_pc) {
                        $NG.alert("请先选择项目");
                        return false;
                    } else {
                        return true;
                    }
                },
                ...(!!mstform.getItem("phid_pc").getValue() && {
                    clientSqlFilter: {
                        phid_pc: mstform.getItem("phid_pc").getValue(),
                    },
                }),
            });
        })
    }

    // 设置字段是否隐藏
    function fieldSetHidden(params) {
        console.log("深化设计类型监听", mstform.getItem("u_detailed_type").getValue());
        let lock = false;
        if (Number(mstform.getItem("u_detailed_type").getValue()) == 1) {
            lock = true;
        } else {
            lock = false;
        }
        return
        //表体多字段设置属性
        $NG.updateUI((updater) => {
            const fields = ["u_detailed_gp", "u_detailed_gm", "u_revenue_diff", "u_gp_diff"];
            fields.forEach((field) => {
                updater.fieldSetForm.p_form_detailed_design_chg[field].setProps({
                    hidden: lock, //隐藏
                    // required: true, //必输
                    // disabled: true, //只读
                });
            });
        });
    }
    //汇总表体数据到表头
    function caculatedgrid() {

        let sub_cost_detailed_this = 0;
        let sub_cost_detailed = window._catch_shsj_total_amt.u_detailed_sub_cost_this;

        let pur_cost_detailed_this = 0;
        let pur_cost_detailed = window._catch_shsj_total_amt.u_detailed_pur_cost_this;

        let self_cost_detailed_this = 0;
        let self_cost_detailed = window._catch_shsj_total_amt.u_detailed_self_cost_this;


        let cost_detailed_this = 0;
        let cost_detailed = 0;

        let u_cnt_revenue = mstform.getItem("u_cnt_revenue").getValue()
        let u_equipment_list_impl_amt = mstform.getItem("u_equipment_list_impl_amt").getValue();
        let u_equipment_list_svc_amt = mstform.getItem("u_equipment_list_svc_amt").getValue();
        let u_sec_dev_amt = mstform.getItem("u_sec_dev_amt").getValue();
        let u_zb_svc_amt = mstform.getItem("u_zb_svc_amt").getValue()
        let u_signed_gp = mstform.getItem("u_signed_gp").getValue()

        const Rows1 = $NG.getCmpApi("p_form_detailed_design_dchg").getRows();
        Rows1.forEach((items) => {
            sub_cost_detailed_this += parseFloat(items.u_sub_detailed_cost_this) || 0;
            sub_cost_detailed += parseFloat(items.u_sub_detailed_cost) || 0;

            pur_cost_detailed_this += parseFloat(items.u_pur_cost_detailed_this) || 0;
            pur_cost_detailed += parseFloat(items.u_pur_cost_detailed) || 0;

            self_cost_detailed_this += parseFloat(items.u_self_cost_detailed_this) || 0;
            self_cost_detailed += parseFloat(items.u_self_cost_detailed) || 0;
        });

        // 深化成本累计值
        sub_cost_detailed += sub_cost_detailed_this;
        pur_cost_detailed += pur_cost_detailed_this;
        self_cost_detailed += self_cost_detailed_this;

        // 本次深化成本
        cost_detailed_this = sub_cost_detailed_this + pur_cost_detailed_this + self_cost_detailed_this;
        // 累计深化成本
        cost_detailed = sub_cost_detailed + pur_cost_detailed + self_cost_detailed;

        // 深化毛利
        u_detailed_gp = u_cnt_revenue - cost_detailed - u_equipment_list_impl_amt - u_equipment_list_svc_amt - u_sec_dev_amt - u_zb_svc_amt
        // 深化毛利率
        u_detailed_gm = (u_detailed_gp + u_equipment_list_svc_amt) / u_cnt_revenue
        console.log(`测试深化毛利率`,u_detailed_gp,u_equipment_list_svc_amt,u_cnt_revenue);
        // 毛利差额
        u_gp_diff = u_signed_gp - u_detailed_gp
        $NG.updateState((updater) => {
            updater.data.p_form_detailed_design_chg.setProps({
                u_detailed_sub_cost: sub_cost_detailed, //
                u_detailed_sub_cost_this: sub_cost_detailed_this, //

                u_detailed_pur_cost_this: pur_cost_detailed_this, //
                u_detailed_pur_cost: pur_cost_detailed, //

                u_detailed_self_cost_this: self_cost_detailed_this, //
                u_detailed_self_cost: self_cost_detailed, //

                u_detailed_gp: u_detailed_gp,
                u_detailed_gm: u_detailed_gm,
                u_gp_diff:u_gp_diff,

                u_detailed_total_cost_this: cost_detailed_this,
                u_detailed_total_cost: cost_detailed,
            });
        });
        // console.log('u_total_cost_amt_vat:' + mstform.getItem('u_total_cost_amt_vat').getValue());
    }

    // 获取深化设计累计值
    function getShsjHeadInfo() {
        const phid_pc = mstform.getItem("phid_pc").getValue();
        const phid = mstform.getItem("u_shsjqd").getValue()
        if (!phid_pc || !phid) return;
        $NG.execServer("getShsjHeadInfo", { phid_pc, phid }, (res) => {
            if (res.count == 0) return;
            const datas = JSON.parse(res.data)[0].extendObjects;
            window._catch_shsj_total_amt = { ...datas };
        });
    }

    //深化设计原有数据
    function getShsjInfoDetail() {
        const phid = mstform.getItem("u_shsjqd").getValue()
        $NG.execServer("getShsjInfoDetail", { phid }, (res) => {
            if (res.count == 0) return
            const _data = JSON.parse(res.data)
            const formData = _data[0].extendObjects
            $NG.updateState((updater) => {
                updater.data.p_form_detailed_design_chg.setProps({
                    u_detailed_total_cost_ori: formData.u_detailed_total_cost_this,
                    u_detailed_sub_cost_ori: formData.u_detailed_sub_cost_this,
                    u_detailed_pur_cost_ori: formData.u_detailed_pur_cost_this,
                    u_detailed_self_cost_ori: formData.u_detailed_self_cost_this,
                    u_equipment_list_total_cost: formData.u_equipment_list_total_cost,
                    u_equipment_list_sub_cost: formData.u_equipment_list_sub_cost,
                    u_equipment_list_pur_cost: formData.u_equipment_list_pur_cost,
                    u_equipment_list_self_cost: formData.u_equipment_list_self_cost,
                    u_equipment_list_impl_amt: formData.u_equipment_list_impl_amt,
                    u_equipment_list_svc_amt: formData.u_equipment_list_svc_amt,
                    u_sec_dev_amt: formData.u_sec_dev_amt,
                    u_signed_gp: formData.u_signed_gp,
                    u_signed_gm: formData.u_signed_gm,
                    u_detailed_gp: formData.u_detailed_gp,
                    u_detailed_gm: formData.u_detailed_gm,
                    u_revenue_diff: formData.u_revenue_diff,
                    u_gp_diff: formData.u_gp_diff,
                })
            })
            const _arr = _data.reduce((value, item) => {
                const el = item.extendObjects
                value.push({
                    u_source_phid: el.dphid,
                    u_pc_subitem_category: el.u_pc_subitem_category,
                    u_equipment_list_no: el.u_equipment_list_no,
                    u_equipment_item: el.u_equipment_item,
                    u_equipment_item_EXName: el.u_equipment_item_exname,
                    u_cnt_d_no: el.u_cnt_d_no,
                    u_cnt_d: el.u_cnt_d,
                    u_cnt_d_EXName: el.u_cnt_d_exname,
                    u_msunit: el.u_unit,
                    u_msunit_EXName: el.u_unit_exname,
                    u_cnt_d_spec: el.u_cnt_d_spec,
                    u_desc: el.u_desc,
                    u_brand_name: el.u_brand_name,
                    u_cnt_d_taxrate: el.u_cnt_d_taxrate,
                    u_cnt_qty: el.u_cnt_qty,
                    u_cnt_d_prc: el.u_cnd_d_prc,
                    u_cnt_d_amt: el.u_cnt_d_amt,
                    u_detailed_qty_this: el.u_detailed_qty_this,
                    u_detailed_qty_ori: el.u_detailed_qty_this,
                    // u_detailed_qty: el.u_detailed_qty,
                    u_work_unit_qty: el.u_work_unit_qty,
                    u_detailed_qty: el.u_u_total_shsj - el.u_detailed_qty_this + el.u_detailed_qty_this,
                    u_sub_item: el.u_sub_item,
                    u_sub_item_EXName: el.u_sub_item_exname,
                    u_sub_item_name: el.u_sub_item_name,
                    u_sub_item_spec: el.u_sub_item_spec,
                    u_sub_item_msunit: el.u_sub_item_msunit,
                    u_sub_item_msunit_EXName: el.u_sub_item_msunit_exname,
                    u_sub_prc: el.u_sub_prc,
                    u_sub_detailed_cost_this: el.u_sub_cost_detailed_this,
                    u_sub_detailed_cost: el.u_sub_cost_detailed,
                    u_pur_item: el.u_pur_item,
                    u_pur_item_EXName: el.u_pur_item_exname,
                    u_pur_item_name: el.u_pur_item_name,
                    u_pur_item_spec: el.u_pur_item_spec,
                    u_pur_item_msunit: el.u_pur_item_msunit,
                    u_pur_item_msunit_EXName: el.u_pur_item_msunit_exname,
                    u_pur_prc: el.u_pur_prc,
                    u_pur_cost_detailed_this: el.u_pur_cost_detailed_this,
                    u_pur_cost_detailed: el.u_pur_cost_detailed,
                    u_self_item_EXName: el.u_self_item_exname,
                    u_self_item_name: el.u_self_item_name,
                    u_self_item_spec: el.u_self_item_spec,
                    u_self_item_msunit: el.u_self_item_msunit,
                    u_self_item_msunit_EXName: el.u_self_item_msunit_exname,
                    u_self_prc: el.u_self_prc,
                    u_self_cost_detailed_this: el.u_self_cost_detailed_this,
                    u_self_cost_detaile: el.u_self_cost_detailed,
                    u_self_cost_detailed_ori: el.u_self_cost_detailed,
                    _init_total_qty: el.u_u_total_shsj
                })
                return value
            }, [])
            dgrid.clearRows().then(() => {
                dgrid.addRows(_arr)
            })
        })
    }


    function getEqDInfoByphid(items) {
        console.log("触发方法: getEqDInfoByphid");

        return new Promise((resolve) => {
            if (items.u_equipment_list_no) { resolve(); return }
            if (items.u_equipment_item && !items.u_equipment_list_no) {
                $NG.execServer("getEqDInfoByphid", { phid: items.u_equipment_item }, (res) => {
                    if (res.count > 0) {
                        const data = JSON.parse(res.data)[0].extendObjects;
                        console.log("getEqDInfoByphid", data);
                        items.u_revenue_type = data.u_revenue_type; // 收入类型
                        items.u_revenue_type_EXName = data.u_revenue_type_name;
                        items.u_pc_subitem_category = data.u_pc_subitem_category;
                        items.u_equipment_list_no = data.u_equipment_list_no; // 设备清单编码
                        items.u_cnt_d_no = data.u_cnt_item_no; // 合同清单编码
                        items.u_cnt_d = data.u_cnt_item; // 合同清单名称
                        items.u_cnt_d_EXName = data.u_cnt_item_name; // 合同清单名称
                        items.u_unit = data.u_msunit;
                        items.u_unit_EXName = data.u_msunit_name; // 计量单位名称
                        items.u_cnt_d_spec = data.u_spec; // 规格型号
                        items.u_desc = data.u_tech_params; // 技术参数
                        items.u_brand_name = data.u_brand_name; // 品牌
                        items.u_cnt_d_taxrate = data.u_cnt_item_tax_rate; // 税率
                        items.u_cnt_qty = data.u_qty; // 合同清单数量
                        items.u_cnd_d_prc = data.u_cnt_prc; // 未税单价
                        items.u_cnt_d_amt = data.u_cnt_amt; // 未税金额
                        items.u_sub_item = data.u_sub_item;
                        items.u_sub_item_EXName = data.u_sub_item_name;
                        items.u_sub_item_name = data.u_sub_item_name;
                        items.u_sub_item_spec = data.u_sub_ggxh;
                        items.u_sub_item_msunit = data.u_sub_item_msunit;
                        items.u_sub_item_msunit_EXName = data.u_subunit_name;
                        items.u_sub_prc = data.u_sub_prc;
                        items.u_sub_cost = data.u_sub_cost;
                        items.u_pur_item = data.u_pur_item;
                        items.u_pur_item_EXName = data.u_pur_item_name;
                        items.u_pur_item_name = data.u_pur_item_name;
                        items.u_pur_item_spec = data.u_pur_ggxh;
                        items.u_pur_item_msunit = data.u_pur_item_msunit;
                        items.u_pur_item_msunit_EXName = data.u_purunit_name;
                        items.u_pur_prc = data.u_pur_prc;
                        items.u_pur_cost = data.u_pur_cost;
                        items.u_self_item = data.u_self_item;
                        items.u_self_item_EXName = data.u_self_item_name;
                        items.u_self_item_name = data.u_self_name;
                        items.u_self_item_spec = data.u_self_ggxh;
                        items.u_self_item_msunit = data.u_self_item_msunit;
                        items.u_self_item_msunit_EXName = data.u_purunit_name;
                        items.u_self_prc = data.u_self_prc;
                        items.u_self_cost = data.u_self_cost;
                    } else {
                        console.log("功能扩展为空: getEqDInfoByphid");
                    }
                    resolve(); // **确保 `Promise` 结束**
                    dgrid.refreshView()
                });
            } else {
                dgrid.refreshView()
                resolve(); // **确保 `Promise` 结束**
            }
        });
    }
    // 获取设备清单数据
    function getEqInfoByPhid_pc() {
        const phid_pc = mstform.getItem("phid_pc").getValue()
        if (!phid_pc) return
        $NG.execServer("getEqInfoByPhid_pc", { phid_pc }, (res) => {
            console.log(`获取数据:getEqInfoByPhid_pc`, res);
            if (res.count > 0) {
                const datas = JSON.parse(res.data)[0].extendObjects;
                // console.log(datas);
                $NG.updateState((updater) => {
                    updater.data.p_form_detailed_design_chg.setProps({
                        u_equipment_list_total_cost: datas.u_total_cost_amt,
                        u_equipment_list_sub_cost: datas.u_sub_cost_amt,
                        u_equipment_list_pur_cost: datas.u_pur_cost_amt,
                        u_equipment_list_self_cost: datas.u_self_cost,
                        u_equipment_list_impl_amt: datas.u_implement_amt,
                        u_equipment_list_svc_amt: datas.u_svc_cost_amt,
                        u_sec_dev_amt: datas.u_sec_dev_amt,
                        u_signed_gp: datas.u_revenue_cost_gm, //签单毛利
                        u_signed_gm: datas.u_signed_gm_rate, // 签单毛利率
                        u_zb_svc_amt: datas.u_win_bid_svc_amt
                    });
                });
            }
        });
    }
});
//获取合同数量
function getCntCount(table, key, fun, otherFun) {
    if (!$NG.getCmpApi(table).getItem("phid_pc").getValue()) return;
    $NG.execServer("getCntCount", { type: 1, phid_pc: $NG.getCmpApi(table).getItem("phid_pc").getValue() }, (res) => {
        console.log(res, "count");
        switch (res.count) {
            case 0:
                return;
            case 1:
                const datas = JSON.parse(res.data)[0].extendObjects;
                const temp_map = {};
                temp_map[key] = datas.phid;
                temp_map[`${key}_EXName`] = datas.bill_name;
                $NG.updateState((updater) => {
                    updater.data[table].setProps({
                        ...temp_map,
                    });
                    setTimeout(() => {
                        fun();
                        if (otherFun) {
                            otherFun();
                        }
                    }, 200);
                });
        }
    });
}
